package com.duoec.dw.lineage.dto;

import com.duoec.dw.lineage.service.LineageTableService;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * sqoop import操作涉及的参数字段
 *
 * @author xuwenzhen
 */
public class SqoopCommendArgument {
    private static final String STR_DOT = ".";

    /**
     * MySQL库名
     */
    private String mySqlDatabase;

    /**
     * MySQL表名
     */
    private String mySqlTable;

    /**
     * 字段
     */
    private String columns;

    /**
     * 数据源-条件
     */
    private String where;

    /**
     * Hive库名
     */
    private String hiveDatabase;

    /**
     * Hive表名
     */
    private String hiveTable;

    /**
     * 查询语句
     */
    private String query;

    /**
     * 分区键值
     */
    private List<String> partitionKeys;

    /**
     * 分区键
     */
    private List<String> partitionValues;

    public String getMySqlDatabase() {
        return mySqlDatabase;
    }

    public void setMySqlDatabase(String mySqlDatabase) {
        this.mySqlDatabase = mySqlDatabase;
    }

    public String getMySqlTable() {
        return mySqlTable;
    }

    public void setMySqlTable(String mySqlTable) {
        this.mySqlTable = mySqlTable;
    }

    public String getColumns() {
        return columns;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getHiveDatabase() {
        return hiveDatabase;
    }

    public void setHiveDatabase(String hiveDatabase) {
        this.hiveDatabase = hiveDatabase;
    }

    public String getHiveTable() {
        return hiveTable;
    }

    public void setHiveTable(String hiveTable) {
        this.hiveTable = hiveTable;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String toSql(boolean importStatement) {
        StringBuilder sb = new StringBuilder("SELECT ");

        String tableId;
        if (importStatement) {
            tableId = mySqlDatabase + STR_DOT + mySqlTable;
        } else {
            tableId = hiveDatabase + STR_DOT + hiveTable;
        }

        if (columns == null) {
            //尝试直接获取列名
            TableInfoDto tableInfo = LineageTableService.getTable(tableId);
            if (tableInfo != null && !CollectionUtils.isEmpty(tableInfo.getFields())) {
                String fields = tableInfo.getFields().stream().map(TableFieldDto::getColName).collect(Collectors.joining(","));
                sb.append(fields);
            } else {
                sb.append("*");
            }
        } else {
            sb.append(columns);
        }
        sb.append(" FROM ").append(tableId);
        if (where != null) {
            sb.append(" WHERE ").append(where);
        }
        return sb.toString();
    }

    public List<String> getPartitionKeys() {
        return partitionKeys;
    }

    public void setPartitionKeys(List<String> partitionKeys) {
        this.partitionKeys = partitionKeys;
    }

    public List<String> getPartitionValues() {
        return partitionValues;
    }

    public void setPartitionValues(List<String> partitionValues) {
        this.partitionValues = partitionValues;
    }
}
